package com.huanmeiqi.demo.persistence.two.dao;

import com.huanmeiqi.demo.persistence.two.DatabaseType;
import com.huanmeiqi.demo.persistence.two.annotation.BindDataSource;
import com.huanmeiqi.demo.persistence.two.domain.TOrder;
import com.huanmeiqi.demo.persistence.two.mapper.TOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

/**
 * @author cq.Wang
 * @date 2018/6/14 10:02
 * @description
 */
@Repository
public class TOrderDAO {

    @Autowired
    private TOrderMapper tOrderMapper;

    /**
     * 插入使用写库
     *
     * @param tOrder
     * @return
     */
    @BindDataSource(value = DatabaseType.WRITE_DB)
    @CachePut(value = "tOrderCache", key = "#result.id", unless = "#result.orderType.equals('兑换')")
    // redis会以tOrderCache::id的键形式存储
    public TOrder insertTOrder(TOrder tOrder) {
//        DBContextHolder.setDatabaseType(DatabaseType.WRITE_DB);
        System.err.println("---->执行insert");
        tOrderMapper.insertSelective(tOrder);
        System.err.println("---->iD" + tOrder.getId());
        return tOrder;
    }

    /**
     * 查询使用读库
     *
     * @param
     * @return
     */
    @BindDataSource(value = DatabaseType.READ_DB)
    @Cacheable(value = "tOrderCache", condition = "#id != 14")
    public TOrder selectTOrder(Long id) {
//        DBContextHolder.setDatabaseType(DatabaseType.READ_DB);
        System.err.println("---->执行select");
        return tOrderMapper.selectByPrimaryKey(id);
    }


}
